package com.yellowpage.wf;  
  
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;   

import com.yellowpage.wf.data.Resource;
import com.yellowpage.wf.service.ResourceService;  
import org.apache.struts2.ServletActionContext;   
  
import com.opensymphony.xwork2.ActionSupport;   
  
public class DownloadAction extends ActionSupport {   
    //下载文件原始存放路径   
    //private final static String DOWNLOADFILEPATH="F:/Project/";   
    //文件名参数变量   
    private String fileName;   
    private Resource resource;
    private ResourceService resourceService;
  
    public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public String getFileName() {   
        return fileName;   
    }   
  
    public void setFileName(String fileName) {   
        this.fileName = fileName;   
    }   
  
    //从下载文件原始存放路径读取得到文件输出流   
    public InputStream getDownloadFile() throws FileNotFoundException,IOException {
    	Resource resource = new Resource();
    	resource = resourceService.getResource(new Long(fileName));
    	InputStream in = new FileInputStream(new File(resource.getDir()));
        return in;   
    }   
    //如果下载文件名为中文，进行字符编码转换   
    public String getDownloadChineseFileName() {
    	Resource resource = new Resource();
    	resource = resourceService.getResource(new Long(fileName));
        String downloadChineseFileName = resource.getName();   
  
        try {   
            downloadChineseFileName = new String(downloadChineseFileName.getBytes(), "ISO8859-1");   
        } catch (UnsupportedEncodingException e) {   
            e.printStackTrace();   
        }   
  
        return downloadChineseFileName;   
    }  
    
    public String getDownloadType() {
    	Resource resource = new Resource();
    	resource = resourceService.getResource(new Long(fileName));
        String downloadType = resource.getType(); 
        return downloadType;
    }
  
    public String execute() {   
        return SUCCESS;   
    }   
//  public static void main(String[] args){
//	String p = "F:/Project/";
//	String f = "20110425064641703.txt";
//	InputStream in = new ByteArrayInputStream((p+f).getBytes());
//	try{
//	int a = in.read(); 
//	System.out.println("aaaa====="+a);
//	}catch(FileNotFoundException ex) {   
//        ex.printStackTrace();   
//    } catch (IOException ex) {   
//        ex.printStackTrace();   
//    } 
//  }
}  
